package de.puc.defencetower.view.tui.panel;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import de.puc.defencetower.controller.iGameControllerActionListener;
import de.puc.defencetower.controller.action.BuildTowerAction;
import de.puc.defencetower.controller.action.UpgradeTowerAction;
import de.puc.defencetower.controller.exception.TowerBuildException;
import de.puc.defencetower.controller.exception.TowerUpgradeException;
import de.puc.defencetower.model.Map;

@SuppressWarnings("serial")
public class TowerPanel extends JPanel {

	private JPanel jpanel;

	public TowerPanel(final iGameControllerActionListener controler, final Map map) {
		this.jpanel = this;
		
		JLabel label = new JLabel("X-pos");
		add(label);
		final TextField tvx = new TextField("0");
		add(tvx);

		label = new JLabel("y-pos");
		add(label);
		final TextField tvy = new TextField("0");
		add(tvy);

		label = new JLabel("TowerTyp(0-2)");
		add(label);
		final TextField tvt = new TextField("0");
		add(tvt);

		JButton buildButton = new JButton("build");
		JButton upgradeButton = new JButton("upgrade");

		buildButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int x = Integer.parseInt(tvx.getText());
				int y = Integer.parseInt(tvy.getText());
				int t = Integer.parseInt(tvt.getText());
				try {
					controler.buildTower(new BuildTowerAction(x, y, t, this));
				} catch (TowerBuildException e1) {
					JOptionPane.showMessageDialog(jpanel, e1.getMessage());
				}

			}
		});
		
		upgradeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int x = Integer.parseInt(tvx.getText());
				int y = Integer.parseInt(tvy.getText());
				try {
					controler.upgradeTower(new UpgradeTowerAction(x, y,this, map));
				} catch (TowerUpgradeException e1) {
					JOptionPane.showMessageDialog(jpanel, e1.getMessage());
				}
			}
		});

		add(buildButton);
		add(upgradeButton);
	}

}
